package com.instalinks.android.apps.smspostservice;

import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Hashtable;

import org.apache.http.util.EncodingUtils;

import android.graphics.Bitmap;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.instalinks.android.apps.smspostservice.helper.ConfigParams;

public class WebActivity extends AppTabBaseActivity {

	private WebView webView;
	private ProgressBar progressBar;

	private MenuItem mnuBack;
	private MenuItem mnuForward;
	private MenuItem mnuReload;
	
	protected void doInitialize() {
		mAppCfg.rePopulate();

		mnuBack = null;
		mnuForward = null;
		mnuReload = null;
		
		setContentView(R.layout.web);

		this.webView = (WebView) findViewById(R.id.wb_webView);
		this.progressBar = (ProgressBar) findViewById(R.id.wb_progressBar);

		setWebViewSettings();

		this.loadHelpUrl();
	}

	protected void doPostInitialize() {
	}

	@Override
	protected void handleMenuDisplay(Menu menu) {
		if(mnuBack == null)
			mnuBack = menu.findItem(R.id.mnuBack);
		
		if(mnuForward == null)
			mnuForward = menu.findItem(R.id.mnuForward);

		if(mnuReload == null)
			mnuReload = menu.findItem(R.id.mnuReload);

		menu.findItem(R.id.mnuSettings).setVisible(false);
		menu.findItem(R.id.mnuUserConfig).setVisible(false);

		mnuBack.setVisible(true).setEnabled(webView.canGoBack());
		mnuForward.setVisible(true).setEnabled(webView.canGoForward());
		mnuReload.setVisible(true).setEnabled(true);
	}

	@Override
	protected boolean handleMenuClick(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.mnuBack:
			this.webView.goBack();
			break;
		case R.id.mnuForward:
			this.webView.goForward();
			break;
		case R.id.mnuReload:
			this.webView.reload();
			break;
		default:
			return super.handleMenuClick(item);
		}

		return true;
	}

	@Override
	protected boolean doBackgroundWork() {
		return true;
	}

	@Override
	public void onBackPressed() {
		if (mnuBack!=null && mnuBack.isEnabled()) {
			this.webView.goBack();
		}
		
		return;
	}
	
	
	private void setWebViewSettings() {

		this.webView.clearHistory();
		this.webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		this.webView.getSettings().setJavaScriptEnabled(true);

		webView.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int progress) {
				progressBar.setProgress(progress);
			}
		});

		webView.setWebViewClient(new WebViewClient() {

			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				progressBar.setProgress(0);
				progressBar.setVisibility(View.VISIBLE);
				
				if(mnuBack!=null)
					mnuBack.setEnabled(view.canGoBack());
				
				if(mnuForward!=null)
					mnuForward.setEnabled(view.canGoForward());

				if(mnuReload!=null)
					mnuReload.setEnabled(false);

			}

			public void onPageFinished(WebView view, String url) {
				// mAppCfg.alert("Finished Loading");
				progressBar.setVisibility(View.GONE);
				
				if(mnuBack!=null)
					mnuBack.setEnabled(view.canGoBack());
				
				if(mnuForward!=null)
					mnuForward.setEnabled(view.canGoForward());
				
				if(mnuReload!=null)
					mnuReload.setEnabled(true);

			}

//			@Override
//			public boolean shouldOverrideUrlLoading(WebView view, String url) {
//				view.loadUrl(url);
//				return true;
//			}

			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				Toast.makeText(getApplicationContext(),
						"Oh no! " + description, Toast.LENGTH_SHORT).show();
			}
		});
	}

	private void loadHelpUrl() {
		StringBuilder params = new StringBuilder();

		Hashtable<String, String> htParams = mAppCfg.getDataPostHashtable();
		htParams.put(ConfigParams.mode.toString(),
				ConfigParams.modes.help.toString());

		Enumeration<String> keys = htParams.keys();
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			String value = htParams.get(key);

			if (params.length() > 0) {
				params = params.append("&");
			}

			params = params.append(key).append("=")
					.append(URLEncoder.encode(value));
		}

		byte[] postData = EncodingUtils.getBytes(params.toString(), "base64");

		this.webView.postUrl(mAppCfg.getDataPostUrl(), postData);

		// this.webView.loadUrl(mAppCfg.getDataPostUrl() + "?mode="
		// + ConfigParams.modes.help.toString());
	}

	@Override
	protected void doOnSuccess() {
	}

	@Override
	protected void doOnFailure() {
	}
}
